package com.work.yshop.mvp.presenter;

import android.content.Context;

import com.work.yshop.model.Order;
import com.work.yshop.mvp.contract.OrderContract;
import com.work.yshop.mvp.model.OrderModel;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * 订单管理Presenter - MVP架构
 */
public class OrderPresenter implements OrderContract.Presenter {

    private WeakReference<OrderContract.View> viewRef;
    private OrderContract.Model model;

    public OrderPresenter(Context context) {
        this.model = new OrderModel(context);
    }

    @Override
    public void attachView(OrderContract.View view) {
        viewRef = new WeakReference<>(view);
    }

    @Override
    public void detachView() {
        if (viewRef != null) {
            viewRef.clear();
            viewRef = null;
        }
    }

    @Override
    public void loadOrders(int status) {
        if (getView() == null) return;

        getView().showLoading();
        List<Order> orders = model.getOrderList(status);
        
        if (orders == null || orders.isEmpty()) {
            getView().showEmptyView(status);
        } else {
            getView().showOrders(orders);
        }
        
        getView().hideLoading();
    }

    @Override
    public void addOrder(String buyerName, double amount) {
        if (getView() == null) return;

        Order newOrder = createNewOrder(buyerName, amount);
        boolean success = model.addOrder(newOrder);
        
        if (success) {
            getView().showOperationResult(true, "订单添加成功");
            loadOrders(0); // 重新加载全部订单
        } else {
            getView().showOperationResult(false, "订单添加失败");
        }
    }

    @Override
    public void acceptOrder(Order order) {
        if (getView() == null || order == null) return;

        order.setStatus(Order.STATUS_PROCESSING);
        boolean success = model.updateOrderStatus(order, Order.STATUS_PROCESSING);
        
        if (success) {
            getView().showOperationResult(true, "已接受订单: " + order.getOrderNumber());
            loadOrders(getLastLoadedStatus());
        } else {
            getView().showOperationResult(false, "订单状态更新失败");
        }
    }

    @Override
    public void shipOrder(Order order) {
        if (getView() == null || order == null) return;

        order.setStatus(Order.STATUS_SHIPPED);
        boolean success = model.updateOrderStatus(order, Order.STATUS_SHIPPED);
        
        if (success) {
            getView().showOperationResult(true, "已发货订单: " + order.getOrderNumber());
            loadOrders(getLastLoadedStatus());
        } else {
            getView().showOperationResult(false, "订单状态更新失败");
        }
    }

    @Override
    public void cancelOrder(Order order) {
        if (getView() == null || order == null) return;

        order.setStatus(Order.STATUS_CANCELED);
        boolean success = model.updateOrderStatus(order, Order.STATUS_CANCELED);
        
        if (success) {
            getView().showOperationResult(true, "已取消订单: " + order.getOrderNumber());
            loadOrders(getLastLoadedStatus());
        } else {
            getView().showOperationResult(false, "订单状态更新失败");
        }
    }

    @Override
    public void deleteOrder(Order order) {
        if (getView() == null || order == null) return;

        boolean success = model.deleteOrder(order);
        
        if (success) {
            getView().showOperationResult(true, "已删除订单: " + order.getOrderNumber());
            loadOrders(getLastLoadedStatus());
        } else {
            getView().showOperationResult(false, "订单删除失败");
        }
    }

    /**
     * 获取当前绑定的View
     */
    private OrderContract.View getView() {
        return viewRef != null ? viewRef.get() : null;
    }

    /**
     * 获取上次加载的订单状态
     */
    private int getLastLoadedStatus() {
        // 这里应该实现获取上次加载状态的逻辑
        // 简单起见，默认返回全部订单状态
        return 0;
    }

    /**
     * 创建新订单
     */
    private Order createNewOrder(String buyerName, double amount) {
        // 生成订单号
        String orderNumber = "O" + System.currentTimeMillis();
        
        // 创建订单对象
        return new Order(
                orderNumber,
                buyerName,
                getCurrentTimeString(),
                amount,
                Order.STATUS_PENDING
        );
    }

    /**
     * 获取当前时间字符串
     */
    private String getCurrentTimeString() {
        return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm", java.util.Locale.getDefault())
                .format(new java.util.Date());
    }
} 